package health.carousel.parsers;

import health.carousel.data.ErrorContainer;
import health.carousel.data.HtmlSafeString;
import health.carousel.data.PositionDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.MessagingException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PositionDetailsParser {
    private static final Logger log = LoggerFactory.getLogger(PositionDetailsParser.class);


    public List<PositionDetails> parseContentDetails(HtmlSafeString content, ErrorContainer errorContainer) throws IOException, MessagingException {
        String seeking = "";
        List<String> listOfLines;
        if(content.isHtml()){
            listOfLines = parseHtmlContent(content.getText());
        } else {
            listOfLines = Arrays.asList(content.getText().split("\\r\\n"));
        }

        List<PositionDetails> detailsList = new ArrayList<PositionDetails>();
        for(String line : listOfLines) {
            if(line.contains("seeking")){
                seeking = line;
            } else if(line.trim().length() > 1 && line.contains(" to start ")){
                //currentProcessingLine = line;
                PositionDetails details;
                try{
                    details = parsePositionDetails(line, seeking);
                } catch (Exception ex) {

                    details = new PositionDetails(
                            0,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            "PARSING ERROR: " + ex.getMessage(),
                            line);
                    errorContainer.getFailedMessages().add(line);
                    log.error("Got an exception for line '" + line + "', exception details: " + ex.getMessage(), ex);
                }

                detailsList.add(details);
            }
        }

        return detailsList;
    }

    private List<String> parseHtmlContent(String content) throws IOException {
        final List<String> listOfLines = new ArrayList<String>();

        ParserDelegator parserDelegator = new ParserDelegator();
        HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {
            public void handleText(final char[] data, final int pos) {
                listOfLines.add(new String(data));
            }
            public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) { }
            public void handleEndTag(HTML.Tag t, final int pos) {  }
            public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, final int pos) { }
            public void handleComment(final char[] data, final int pos) { }
            public void handleError(final String errMsg, final int pos) { }
        };
        parserDelegator.parse(new StringReader(content), parserCallback, true);
        return listOfLines;
    }


    public PositionDetails parsePositionDetails(String line, String seeking) throws MessagingException, IOException {

        return new PositionDetails(
                -1,
                "test",
                null,
                null,
                "test",
                "test",
                "test",
                "test",
                "MSN parser",
                line);
    }

}
